package com.demo.controller;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.demo.bean.DongHuan;
import com.demo.bean.Event;
import com.demo.service.JsonManageService;
import com.demo.service.impl.JsonManageServiceImpl;
import com.demo.util.ActiveMQPusher;
import com.demo.util.FormatUtil;
import com.demo.util.HttpSendEvent;
import net.sf.json.JSON;
import net.sf.json.JSONObject;

@Controller
public class EventController {
	/**
	 * itvop自动推送告警
	 * 
	 * @param eventId 设备唯一ID
	 * @param eip     推送ip地址
	 * @return
	 */
	@RequestMapping("/uploadEventItvop")
	public String uploadEventItvop(String eventId, String eip) {

		String classPath = this.getClass().getResource("/").getPath();
		System.out.println("读取数据：" + classPath + "perfsetup.json");

		JsonManageService service = new JsonManageServiceImpl();
		List<String> idList = new ArrayList<String>();
		String EIP = "127.0.0.1";

		if (null != eip && !"".equals(eip)) {
			EIP = eip;
		}
		System.out.println("--------------------------------------------------------------------------------------");
		System.out.print("推送告警id:");
		if (eventId != null) {
			String[] sourceId = eventId.split(",");
			for (int i = 0; i < sourceId.length; i++) {
				if (!"".equals(sourceId[i])) {
					idList.add(sourceId[i]);
					System.out.print(sourceId[i] + "、");
				}
			}
			List<JSON> jsonlist = service.eventManage(idList, classPath + "perfsetup.json");
			ActiveMQPusher perfacmq = new ActiveMQPusher("admin", "admin", "tcp://" + EIP + ":61616", "eventQueue");
			System.out.println("");
			System.out.println("推送告警IP:" + EIP);
			System.out.print("推送告警数据:");
			for (int i = 0; i < jsonlist.size(); i++) {
				System.out.println(jsonlist.get(i).toString());
				perfacmq.push2ActiveMQ(jsonlist.get(i).toString());
			}
			System.gc();
		}
		return "event_succeed.html";
	}

	/**
	 * Tarsier 自动推送告警
	 * 
	 * @param eventId 设备唯一ID
	 * @param eurl    推送NOAH服务地址
	 * @return
	 * @throws FileNotFoundException
	 */
	@RequestMapping("/uploadEventTarsier")
	public String uploadEventTarsier(HttpServletRequest request, String eventId, String eurl)
			throws FileNotFoundException {

		String classPath = this.getClass().getResource("/").getPath();
		System.out.println("读取数据：" + classPath + "perfsetup.json");

		JsonManageService service = new JsonManageServiceImpl();
		HttpSendEvent sendEvent = new HttpSendEvent();
		List<String> idList = new ArrayList<String>();
		List<JSON> jsonlist = new ArrayList<JSON>();
		String jsonstr = null;
		System.out.println("--------------------------------------------------------------------------------------");
		System.out.print("推送告警id:");
		if (eventId != null) {
			String[] sourceId = eventId.split(",");
			for (int i = 0; i < sourceId.length; i++) {
				if (!"".equals(sourceId[i])) {
					idList.add(sourceId[i]);
					System.out.print(sourceId[i] + "、");
				}
			}
			jsonlist = service.eventManage(idList, classPath + "perfsetup.json");
			jsonstr = FormatUtil.formatJson(jsonlist.toString());
			System.out.println("");
			System.out.println("推送告警url:" + eurl);
			// System.out.print("推送告警数据:");
			// System.out.println(jsonlist);
			String resl = sendEvent.pushEp(jsonlist, eurl);
			if (resl != null) {
				JSONObject jsStr = JSONObject.fromObject(resl);
				if (jsStr.getString("code").equals("200")) {
					request.setAttribute("resl", resl);
					request.setAttribute("data", jsonstr);
					request.setAttribute("zhuantai", "成功");
					System.gc();
					return "event_succeed.jsp";
				}
				/*if (jsStr.getString("success").equals("false")) {
					request.setAttribute("resl", resl);
					request.setAttribute("data", jsonstr);
					request.setAttribute("zhuantai", "失败");
					System.gc();
					return "event_succeed.jsp";
				}*/
			}
		}
		request.setAttribute("data", jsonstr);
		System.gc();
		return "event_fail.jsp";
	}

	/**
	 * itvop 自定义参数告警
	 * 
	 * @param eventId 告警对象 ci code
	 * @param eip     推送ip地址
	 * @param content 告警内容
	 * @return
	 */
	@RequestMapping("/uploadEventItvopDIY")
	public String uploadEventItvopDIY(String eventId, String eip, String content) {
		JsonManageService service = new JsonManageServiceImpl();
		List<String> idList = new ArrayList<String>();
		String EIP = "127.0.0.1";

		String classPath = this.getClass().getResource("/").getPath();
		System.out.println("读取数据：" + classPath + "perfsetup.json");

		if (null != eip && !"".equals(eip)) {
			EIP = eip;
		}
		System.out.println("--------------------------------------------------------------------------------------");
		System.out.print("推送告警id:");
		if (eventId != null) {
			String[] sourceId = eventId.split(",");
			for (int i = 0; i < sourceId.length; i++) {
				if (!"".equals(sourceId[i])) {
					idList.add(sourceId[i]);
					System.out.print(sourceId[i] + "、");
				}
			}
			List<JSON> jsonlist = service.eventManages(idList, content, classPath + "perfsetup.json");
			ActiveMQPusher perfacmq = new ActiveMQPusher("admin", "admin", "tcp://" + EIP + ":61616", "eventQueue");
			System.out.println("");
			System.out.println("推送告警IP:" + EIP);
			System.out.print("推送告警数据:");
			for (int i = 0; i < jsonlist.size(); i++) {
				System.out.println(jsonlist.get(i).toString());
				perfacmq.push2ActiveMQ(jsonlist.get(i).toString());
			}
			System.gc();
		}
		return "event_succeed.html";
	}

	/**
	 * tarsier 自定义参数告警
	 * 
	 * @param eventId 告警对象 ci code
	 * @param eurl    推送NOAH服务地址
	 * @param content 告警内容
	 * @return
	 */
	@RequestMapping("/uploadEventTarsierDIY")
	public String uploadEventTarsierDIY(HttpServletRequest request, String eventId, String eurl, String content) {
		JsonManageService service = new JsonManageServiceImpl();
		HttpSendEvent sendEvent = new HttpSendEvent();
		List<String> idList = new ArrayList<String>();
		List<JSON> jsonlist = new ArrayList<JSON>();
		String jsonstr = null;

		String classPath = this.getClass().getResource("/").getPath();
		System.out.println("读取数据：" + classPath + "perfsetup.json");

		System.out.println("--------------------------------------------------------------------------------------");
		System.out.print("推送告警id:");
		if (eventId != null) {
			String[] sourceId = eventId.split(",");
			for (int i = 0; i < sourceId.length; i++) {
				if (!"".equals(sourceId[i])) {
					idList.add(sourceId[i]);
					System.out.print(sourceId[i] + "、");
				}
			}
			jsonlist = service.eventManages(idList, content, classPath + "perfsetup.json");
			jsonstr = FormatUtil.formatJson(jsonlist.toString());
			System.out.println("");
			System.out.println("推送告警url:" + eurl);
			// System.out.print("推送告警数据:");
			// System.out.println(jsonlist);
			String resl = sendEvent.pushEp(jsonlist, eurl);
			if (resl != null) {
				JSONObject jsStr = JSONObject.fromObject(resl);
				if (jsStr.getString("code").equals("200")) {
					request.setAttribute("resl", resl);
					request.setAttribute("data", jsonstr);
					request.setAttribute("zhuantai", "成功");
					System.gc();
					return "event_succeed.jsp";
				}
				/*if (jsStr.getString("success").equals("false")) {
					request.setAttribute("resl", resl);
					request.setAttribute("data", jsonstr);
					request.setAttribute("zhuantai", "失败");
					System.gc();
					return "event_succeed.jsp";
				}*/
			}
		}
		request.setAttribute("data", jsonstr);
		System.gc();
		return "event_fail.jsp";
	}

	@RequestMapping("/eventsetupout")
	public String eventsetupout(HttpServletRequest request) {
		JsonManageService jsonManageService = new JsonManageServiceImpl();
		Event event = new Event();
		String classPath = this.getClass().getResource("/").getPath();
		System.out.println("读取数据：" + classPath + "perfsetup.json");

		event = jsonManageService.readEventSetUpJson(classPath + "perfsetup.json");
		request.setAttribute("event", event);
		return "eventsetup.jsp";
	}

	@RequestMapping("/eventsetupin")
	public String eventsetupin(HttpServletRequest request, Event event) {
		JsonManageService jsonManageService = new JsonManageServiceImpl();

		String classPath = this.getClass().getResource("/").getPath();
		System.out.println("写入数据：" + classPath + "perfsetup.json");

		boolean rel = jsonManageService.writeEventSetUpJson(classPath + "perfsetup.json", event);
		if (rel) {
			request.setAttribute("zhuantai", "✔");
			return "/eventsetupout.action";
		}
		request.setAttribute("zhuantai", "✘");
		return "eventsetup.jsp";
	}
}
